
package edu.upn.sigemfi.plt.bc;

import edu.upn.sigemfi.plt.be.Chequera;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrador
 */
public class ChequeraDelegate {

    private ChequeraFacadeLocal local;

    public ChequeraDelegate() {
        local = lookupChequeraFacade();
    }

    public void crear(Chequera chequera) throws Exception{
        try {
            local.create(chequera);
        } catch (Exception e) {
            local = lookupChequeraFacade();
            throw new Exception("Crear Chequera. " + e.getMessage(),e);
        }
    }

    public void editar(Chequera chequera) throws Exception{
        try {
            local.edit(chequera);
        } catch (Exception e) {
            local = lookupChequeraFacade();
            throw new Exception("Editar Chequera. " + e.getMessage(), e);
        }
    }

    public void eliminar(Chequera chequera) throws Exception{
        try {
            local.remove(chequera);
        } catch (Exception e) {
            local = lookupChequeraFacade();
            throw new Exception("Eliminar Chequera. " + e.getMessage(), e);
        }
    }

    public Chequera buscar(Long codigo) throws Exception{
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupChequeraFacade();
            throw new Exception("Buscar Chequera. " + e.getMessage(), e);
        }
    }

    public List<Chequera> listar() throws Exception{
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupChequeraFacade();
            throw new Exception("Listar Chequera." + e.getMessage(), e);
        }
    }

    private ChequeraFacadeLocal lookupChequeraFacade() {
        try {
            Context c = new InitialContext();
            return (ChequeraFacadeLocal) c.lookup("java:comp/env/ChequeraFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
